#include "animated_sprite.h"


AnimatedSprite::AnimatedSprite(void) {
  
}


AnimatedSprite::AnimatedSprite(int total_texture) {
  // Init frames
  this->currentFrame = 0;
  this->numFrames = total_texture;
  // Create sprite handeler
  HRESULT result = D3DXCreateSprite(
      Screen::screen->getDevive(), 
      &spriteHandler);
  // create new array
  textures = new TexturesManager::TextureNode*[total_texture];
  if(result != D3D_OK) {
     Log::Write(L"Create sprite handeler false.");
     return;
  }
}


AnimatedSprite::~AnimatedSprite(void) {
  
}


void AnimatedSprite::NextFrame() {
  this->currentFrame++;
  if(this->currentFrame >= this->numFrames)
    this->currentFrame = 0;
}


void AnimatedSprite::SetTexture(int textureId, int index) {
  this->textures[index] = TexturesManager::getInstance()->GetTextureById(textureId);
}


void AnimatedSprite::Draw(RECT *rect, D3DXVECTOR3 *vector_position) {
  // Lock sprite object for drawing
  this->spriteHandler->Begin(D3DXSPRITE_ALPHABLEND);
  this->spriteHandler->Draw(
    // draw current texture
    this->textures[currentFrame]->texture,
    rect,
    NULL,
    vector_position,
    D3DCOLOR_XRGB(255,255,255));
  // Unlock sprte after drawing
  this->spriteHandler->End();
}


